//
//  ATScript.m
//  AutomationTool
//
//  Created by Laurie Marmon on 2/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ATScript.h"

#pragma mark Private Properties

@interface ATScript ()

@property (nonatomic, strong) NSMutableArray *setupStepsStorage;
@property (nonatomic, strong) NSMutableArray *normalStepsStorage;
@property (nonatomic, strong) NSMutableArray *resetStepsStorage;
@property (nonatomic, readwrite) NSString *displayString;

@end

#pragma mark Main Class Implementation

@implementation ATScript

@synthesize name = _name;
@synthesize userDescription = _userDescription;
@synthesize setupStepsStorage = _setupStepsStorage;
@synthesize normalStepsStorage = _normalStepsStorage;
@synthesize resetStepsStorage = _resetStepsStorage;
@synthesize isArchived = _isArchived;
@synthesize displayString = _displayString;

- (id)init
{
    self = [super init];
    if (self) {
        self.name = @"New Script";
        self.setupStepsStorage = [[NSMutableArray alloc] init];
        self.normalStepsStorage = [[NSMutableArray alloc] init];
        self.resetStepsStorage = [[NSMutableArray alloc] init];
        self.displayString = [self description];
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"SetupSteps=%@\n NormalSteps=%@\n ResetSteps=%@", self.setupStepsStorage.description, self.normalStepsStorage.description, self.resetStepsStorage.description];
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_name forKey:@"ATScriptName"];
    [aCoder encodeObject:_userDescription forKey:@"ATScriptUserDescription"];
    [aCoder encodeObject:_setupStepsStorage forKey:@"ATScriptSetupStorage"];
    [aCoder encodeObject:_normalStepsStorage forKey:@"ATScriptNormalStorage"];
    [aCoder encodeObject:_resetStepsStorage forKey:@"ATScriptResetStorage"];
    [aCoder encodeBool:_isArchived forKey:@"ATScriptIsArchived"];
    [aCoder encodeObject:_displayString forKey:@"ATScriptDisplayString"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    _name = [aDecoder decodeObjectForKey:@"ATScriptName"];
    _userDescription = [aDecoder decodeObjectForKey:@"ATScriptUserDescription"];
    _setupStepsStorage = [aDecoder decodeObjectForKey:@"ATScriptSetupStorage"];
    _normalStepsStorage = [aDecoder decodeObjectForKey:@"ATScriptNormalStorage"];
    _resetStepsStorage = [aDecoder decodeObjectForKey:@"ATScriptResetStorage"];
    _isArchived = [aDecoder decodeBoolForKey:@"ATScriptIsArchived"];
    _displayString = [aDecoder decodeObjectForKey:@"ATScriptDisplayString"];
    return self;
}

- (void)refreshScriptUI
{
    self.displayString = [self description];
}

- (NSMutableArray *)storageForScriptStepType:(ATScriptStepType)type
{
    if (type == ATScriptStepTypeSetup) {
        return self.setupStepsStorage;
    } else if (type == ATScriptStepTypeNormal) {
        return self.normalStepsStorage;
    } else if (type == ATScriptStepTypeReset) {
        return self.resetStepsStorage; 
    } else {
        NSLog(@"Unknown type encountered");
        return nil;
    }
}

- (void)insertScriptStep:(ATScriptStep *)scriptStep atIndex:(NSUInteger)index ofType:(ATScriptStepType)type
{
    if (scriptStep) {
        NSMutableArray *storage = [self storageForScriptStepType:type];
        [self willChangeValueForKey:@"scriptSteps"];
        [storage insertObject:scriptStep atIndex:index];
        [self didChangeValueForKey:@"scriptSteps"];
        [self refreshScriptUI];
    }
}

- (void)removeScriptStep:(ATScriptStep *)scriptStep ofType:(ATScriptStepType)type
{
    if (scriptStep) {
        NSMutableArray *storage = [self storageForScriptStepType:type];
        [self willChangeValueForKey:@"scriptSteps"];
        [storage removeObject:scriptStep];
        [self didChangeValueForKey:@"scriptSteps"];
        [self refreshScriptUI];
    }
}

- (void)removeAllScriptStepsofType:(ATScriptStepType)type
{
    NSMutableArray *storage = [self storageForScriptStepType:type];
    [self willChangeValueForKey:@"scriptSteps"];
    [storage removeAllObjects];
    [self didChangeValueForKey:@"scriptSteps"];
    [self refreshScriptUI];
}

- (void)reorderScriptStep:(ATScriptStep *)scriptStep atIndex:(NSUInteger)index ofType:(ATScriptStepType)type toIndex:(NSUInteger)newIndex
{
    if (scriptStep) {
        NSMutableArray *storage = [self storageForScriptStepType:type];
        [self willChangeValueForKey:@"scriptSteps"];
        [storage exchangeObjectAtIndex:index withObjectAtIndex:newIndex];
        [self didChangeValueForKey:@"scriptSteps"];
        [self refreshScriptUI];
    }
}

- (void)updateScriptStep:(ATScriptStep *)scriptStep ofType:(ATScriptStepType)type
{
    if (scriptStep) {
        NSMutableArray *storage = [self storageForScriptStepType:type];
        //[self willChangeValueForKey:@"scriptSteps"];
        [storage replaceObjectAtIndex:[storage indexOfObject:scriptStep] withObject:scriptStep];
        //[self didChangeValueForKey:@"scriptSteps"];
        [self refreshScriptUI];
    }
}

- (NSArray *)scriptStepsOfType:(ATScriptStepType)type
{
    return [self storageForScriptStepType:type];
}

- (NSArray *)scriptSteps
{
    NSMutableArray *steps = [[NSMutableArray alloc] init];
    [steps addObjectsFromArray:self.setupStepsStorage];
    [steps addObjectsFromArray:self.normalStepsStorage];
    [steps addObjectsFromArray:self.resetStepsStorage];
    return steps;
}

@end
